﻿Imports System.Media

Public Class Form1
    Private runder As Byte
    Private randomMinutes As Integer
    Private minutes As Byte
    Private seconds As Byte
    Private hideClock As Boolean
    Private gameOver As Boolean
    Private randomRound As Random

    Private Sub showTime()
        Dim secs As Byte
        Dim runde As Integer

        secs = IIf(Me.seconds = 60, 0, Me.seconds)

        If Me.gameOver Then
            Me.clock.ForeColor = Color.Red
            Me.clock.Text = "GAME OVER"
            Call Me.updateRoundInfo()
        ElseIf Me.hideClock Then
            Me.clock.Text = IIf(Me.clock.Text = "", "???", "")
        Else
            Me.clock.Text = CStr(Me.minutes) + ":" + IIf(secs < 10, "0" + CStr(secs), CStr(secs))
        End If
    End Sub

    Private Sub updateRoundInfo()
        Dim runde As Integer
        runde = CInt(Me.AntalRunder.Text) - runder + 1
        If Me.roundInfo.Text <> CStr(runde) Then
            Me.roundInfo.Text = "runde " + CStr(runde)
        End If

        If Me.clock.Text = "GAME OVER" Then
            Me.roundInfo.Visible = False
        Else
            Me.roundInfo.Visible = True
        End If
    End Sub

    Private Sub processTime()
        If Me.seconds = 0 Then
            If Me.minutes = 0 Then
                Call Me.playSoundFromResource()
                Me.runder = Me.runder - 1
                Call Me.updateRoundInfo()

                If Me.runder = 1 Then
                    Me.minutes = Me.randomMinutes
                    Me.hideClock = True
                ElseIf Me.runder = 0 Then
                    Me.Timer1.Stop()
                    Me.gameOver = True
                Else
                    Me.minutes = CLng(Me.interval.Text)
                End If

                Me.seconds = 0
            Else
                Me.seconds = 59
                Me.minutes = Me.minutes - 1
            End If
        Else
            Me.seconds = Me.seconds - 1
        End If
    End Sub

    Private Sub reset()
        Me.clock.Text = ""
        Me.runder = 0
        Me.randomMinutes = 0
        Me.minutes = 0
        Me.seconds = 0
        Me.hideClock = False
        Me.gameOver = False
        Me.Button1.Text = "Start"
        Me.clock.ForeColor = Color.Black
        Me.Timer1.Stop()
    End Sub

    Private Sub playSoundFromResource()
        Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
        Dim s As System.IO.Stream = a.GetManifestResourceStream("tpc_ns.ringin.wav")
        Dim player As SoundPlayer = New SoundPlayer(s)
        player.Play()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.gameOver Then Call Me.reset() 'auto reset v. game over

        If Me.randomMinutes = 0 Then
            Me.randomMinutes = randomRound.Next(CInt(Me.min.Text), CInt(Me.max.Text))
        End If

        If Me.runder = 0 Then
            Me.runder = CByte(Me.AntalRunder.Text)
        End If

        If Me.seconds = 0 And Me.minutes = 0 And Not Me.Timer1.Enabled Then
            Me.minutes = CLng(Me.interval.Text)
        End If

        If Me.Timer1.Enabled Then   '/* tiden kører og der er trykket pause */
            Me.Timer1.Stop()
            Me.Button1.Text = "Start"
        Else                        '/* tiden kører ikke og der er trykket start */
            Me.Button1.Text = "Pause"
            Call Me.showTime()
            Call Me.updateRoundInfo()
            Me.Timer1.Start()
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Call Me.processTime()
        Call Me.showTime()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        randomRound = New Random
        Me.randomMinutes = 0
        Me.Timer1.Interval = 1000
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.reset()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.playSoundFromResource()
    End Sub
End Class
